// pages/new1/new1.js
var common = require("../../utils/public.js") //引入public.js脚本
var api = require('../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    amount:"",
    phone:"",
    userMess:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userMess = JSON.parse(options.userMess)
    this.setData({
      amount: options.amount,
      id: options.id,
      phone: options.phone,
      userMess: userMess
    })
  },
  // 充值按钮
  cz: common.throttleFunc(function (e) {
    let that = this
    api.topUp({
      id: that.data.id,
      phone: that.data.phone
    }).then(res => {
      if (res.result == true) {
        wx.requestPayment({
          timeStamp: res.object.timeStamp,
          nonceStr: res.object.nonce_str,
          package: res.object.packageParams,
          signType: "MD5",
          paySign: res.object.sign,
          success: function (re) {
            api.topUpConfirm({
              orderCode: res.object.out_trade_no
            }).then(res => {
              console.log(res)
              api.get_cart({
                userId: wx.getStorageSync("openId"),
                cityId: wx.getStorageSync("cityId"),
                page: {}
              }).then(res => {
                if (res.data.result) {
                  wx.setStorageSync("userMess", res.data.object)
                  wx.navigateBack({
                    data: 1
                  })
                }
              })
            })
          },
          fail: function (res) {
            console.log(res)
          }
        })
      }
    })

  }),
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})